//
//  FirststartViewController.swift
//  NDAi
//
//  Created by 丸子 on 2024/7/4.
//

import UIKit

class FirststartViewController: UIViewController {
    let viewModel = HomeViewModel()
    
    lazy var logoIV: UIImageView = {
      let imageView = UIImageView()
       imageView.image = UIImage(named: "ai_launch_logo")
       imageView.contentMode = .scaleToFill
       imageView.clipsToBounds = true
       return imageView
   }()
    
    lazy var aiIV: UIImageView = {
      let imageView = UIImageView()
       imageView.image = UIImage(named: "icon_launch_title")
       imageView.contentMode = .scaleToFill
       imageView.clipsToBounds = true
       return imageView
   }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.viewModel.getPlazaList()
        self.view.backgroundColor = .white
        self.view.addSubviews([logoIV, aiIV])
        
        self.logoIV.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(self.safeTop).offset(100)
            make.width.height.equalTo(108)
        }
        
        self.aiIV.snp.makeConstraints { make in
            make.top.equalTo(logoIV.snp.bottom).offset(20)
            make.centerX.equalToSuperview()
        }
        
        let view = AgreementPOPView()
        self.view.addSubview(view)
        view.initViews()
        
        view.chooseDone = { [weak self] in
            UserDefaults.standard.setValue("1", forKey: first)
            if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
                appDelegate.loadWindow()
            }
        }
    }
}

class AgreementPOPView: UIView {
    let disposeBag = DisposeBag()
    var chooseDone: (() -> Void)?
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.text = "个人信息保护指引"
        label.font = UIFont.pingFangSCMedium(ofSize: 16)
        label.textColor = Theme.color.text_d1
        label.textAlignment = .center
        return label
    }()
    
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.textAlignment = .center
        label.font = UIFont.pingFangSCRegular(ofSize: 14)
        label.textColor = Theme.color.text_d3
        label.text = "我们将通过用户协议和隐私政策，帮助您了解我们为您提供的服务、我们如何处理个人信息以及您享有的权利。点击“同意”按钮，表示您已知情并同意以上协议和以下约定。\n1.请你知悉，您同意隐私政策仅代表您已了解应用提供的功能，以及功能运行所必需的个人信息，并不代表您已同意我们可以收集非必要个人信息，非必要个人信息会根据您使用过程中的授权情况单独征求您的同意。\n2.我们尊重您的选择权，您可以访问、修改、删除您的个人信息您的授权，我们也为您提供注销、投诉渠道。"
        return label
    }()
    
    private lazy var cancelButton: UIButton = {
        let button = UIButton()
        button.setTitle("不同意", for: .normal)
        button.setTitleColor(Theme.color.text_d1, for: .normal)
        button.titleLabel?.font = UIFont.pingFangSCRegular(ofSize: 16)
        button.layer.borderWidth = 1
        button.layer.borderColor = Theme.color.text_d1.cgColor
        button.allRound(25)
        return button
    }()
    
    private lazy var confirmButton: UIButton = {
        let button = UIButton()
        button.setTitle("同意并继续", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = UIFont.pingFangSCRegular(ofSize: 16)
        button.backgroundColor = UIColor(hexString: "#25262B")
        button.allRound(25)
        return button
    }()
    
    private lazy var alertView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        view.layer.cornerRadius = 10
        view.layer.masksToBounds = true
        return view
    }()
    
    init() {
        super.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
        self.backgroundColor = UIColor(hexString: "#000000", transparency: 0.3)
        addSubview(alertView)
        bindEvent()
    }
    
    func bindEvent(){
        cancelButton.rx.tap.subscribe(onNext: { [weak self] _ in
            guard let self = self else { return }
            UIView.animate(withDuration: 0.3, animations: {
                self.alertView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
                self.alpha = 0
            },completion: { _ in
                self.removeFromSuperview()
                exit(0)
            })
        }).disposed(by: disposeBag)
        
        confirmButton.rx.tap.subscribe(onNext: { [weak self] _ in
            guard let self = self else { return }
            UIView.animate(withDuration: 0.3, animations: {
                self.alertView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
                self.alpha = 0
            },completion: { _ in
                self.removeFromSuperview()
                self.chooseDone?()
            })
        }).disposed(by: disposeBag)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initViews() {
        alertView.addSubviews([titleLabel, contentLabel, cancelButton, confirmButton])
        
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(24)
        }
        
        contentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(16)
            make.leading.equalToSuperview().offset(20)
            make.trailing.equalToSuperview().offset(-20)
        }
         
        cancelButton.snp.makeConstraints { (make) in
            make.leading.equalToSuperview().offset(16)
            make.top.equalTo(contentLabel.snp.bottom).offset(32)
            make.height.equalTo(50)
        }
        
        confirmButton.snp.makeConstraints { (make) in
            make.trailing.equalToSuperview().offset(-16)
            make.width.equalTo(cancelButton.snp.width)
            make.leading.equalTo(cancelButton.snp.trailing).offset(12)
            make.top.equalTo(contentLabel.snp.bottom).offset(32)
            make.height.equalTo(50)
        }
        
        alertView.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview().inset(24)
            make.centerY.equalToSuperview()
            make.top.greaterThanOrEqualTo(titleLabel.snp.top).offset(-24)
            make.bottom.greaterThanOrEqualTo(confirmButton.snp.bottom).offset(24)
        }
        
        alertView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
        UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
            self.alertView.transform = .identity
        }, completion: nil)
    }
}
